﻿using Super_Management_Mayhem.Content.Animations;
using Super_Management_Mayhem.Content.Spritesheet;
using Transformable_Engine_v2.Engine.Animation.FrameAnimation;
using Transformable_Engine_v2.Engine.Animation.ScaleAnimation;
using Transformable_Engine_v2.Engine.Animation.Trajectory;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.GameObjects.Containers.Transformable_controls;
using Transformable_Engine_v2.Engine.Graphics;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.PlayerSelection
{
    public class Button : Container
    {
        private ImageContainer _buttonHover;
        private ImageContainer _buttonSelected;
        private ScaleAnimation _buttonHoverAnimation;

        public Button(TE_Vector2 position, string text)
        {
            PositionGridBound = position;

            AddChildren(new ImageContainer {
                                               Sprite = MenuSheet.GetSprite(MenuSheet.Button.Background),
                                               Scale = new TE_Vector2(2, 2)
                                           });

            _buttonSelected = new ImageContainer {
                                                     Sprite = MenuSheet.GetSprite(MenuSheet.Button.NotSelected),
                                                     Color = new Color(Color.White, 100),
                                                     Scale = new TE_Vector2(2, 2)
                                                 };

            _buttonHover = new ImageContainer {
                                                  PositionGridBound = new TE_Vector2(8, 2),
                                                  Sprite = MenuSheet.GetSprite(MenuSheet.Button.Hover),
                                                  Origin = TE_Rectangle.Alignment.CenterMiddle,
                                                  Color = Color.Gold,
                                                  Visible = false,
                                                  Scale = new TE_Vector2(2, 2)
                                              };
            _buttonHoverAnimation = new ScaleAnimation(new SinusTrajectory(1), new TE_Vector2(2.2f, 2.4f), 1000, true, 0);
            _buttonHover.AddAnimations(_buttonHoverAnimation);

            AddChildren(new Label(MenuSheet.GetInstance().GetSpriteAlphabet<MenuSheet.LetterAlphabet>, text) {
                                                                                                     PositionGridBound = new TE_Vector2(8, 2),
                                                                                                     TextAlign = TE_Rectangle.Alignment.CenterMiddle,
                                                                                                     Scale = new TE_Vector2(2, 2)
                                                                                                 });

            AddChildren(_buttonSelected, _buttonHover);
        }

        public void Select()
        {
            _buttonSelected.AddAnimations(new FrameAnimation(new ButtonSelected()));
            _buttonHover.Scale = new TE_Vector2(2, 2);
            _buttonHoverAnimation.Reset();
            _buttonHover.Visible = true;
        }

        public void Unselect()
        {
            _buttonSelected.ClearAnimation();
            _buttonHover.Visible = false;
            _buttonSelected.Sprite = MenuSheet.GetSprite(MenuSheet.Button.NotSelected);
        }
    }
}